<?php

class User_Model_RelationshipMap
{
	protected static $_instanceList = array();



	public static function find($firstAccountId, $secondAccountId, $type)
	{
		foreach (self::$_instanceList as $inst) {
			if ($inst->getType() === $type
				&& (($inst->getFirstUserId() == $firstAccountId && $inst->getSecondUserId() == $secondAccountId)
				|| ($inst->getFirstUserId() == $secondAccountId && $inst->getSecondUserId() == $firstAccountId))) {

				return $inst;
			}
		}
		return NULL;
	}
	public static function has($firstAccountId, $secondAccountId, $type, $approvedOnly = TRUE)
	{
		$rel = self::get($firstAccountId, $secondAccountId, $type);
		return $rel === NULL ? FALSE : $approvedOnly ? $rel->getApproveFlag() === TRUE : FALSE;
	}

	public static function get($firstAccountId, $secondAccountId, $type)
	{
		$rel = self::find($firstAccountId, $secondAccountId, $type);
		if ($rel != NULL) {
			return $rel;
		}

		$rels = self::_load($firstAccountId);
		$relId = 0;
		foreach ($rels as $inst) {
			if ($inst->getType() === $type
				&& (($inst->getFirstUserId() == $firstAccountId && $inst->getSecondUserId() == $secondAccountId)
				|| ($inst->getFirstUserId() == $secondAccountId && $inst->getSecondUserId() == $firstAccountId))) {

				$relId = $inst->getId();
				break;
			}
		}

		if ($relId > 0) {
			return self::$_instanceList[$relId];
		}

		$rels = self::_load($secondAccountId);
		$relId = 0;
		foreach ($rels as $inst) {
			if ($inst->getType() === $type
				&& (($inst->getFirstUserId() == $firstAccountId && $inst->getSecondUserId() == $secondAccountId)
				|| ($inst->getFirstUserId() == $secondAccountId && $inst->getSecondUserId() == $firstAccountId))) {

				$relId = $inst->getId();
				break;
			}
		}

		if ($relId > 0) {
			return self::$_instanceList[$relId];
		}

		return NULL;
	}

	public static function set($firstAccountId, $secondAccountId, $type, $approved = TRUE)
	{
		if (intval($firstAccountId) < 1 || intval($secondAccountId) < 1 || empty($type)) {
			throw new Exception('Invalid parameter(s)');
		}

		//TODO: authorization

		$relationship = self::get($firstAccountId, $secondAccountId, $type, FALSE);

		if ($relationship && $relationship->getApproveFlag() == $approved) {
			return $relationship->getId();
		}

		$table = User_Model_DbTable_Relationship::getInstance();

		if ($relationship) {

			$dbData = array(
				'approve_flag' => intval($approved),
			);

			$updRows = $table->update($dbData, array(
				'id= ?' => $firstAccountId,
			));

		} else {

			$dbData = array(
				'first_id' => intval($firstAccountId),
				'second_id' => intval($secondAccountId),
				'type' => $type,
				'approve_flag' => intval($approved),
			);

			$insertId = $table->insert($dbData);

			if ($insertId < 1) {
				throw new Exception('Query error');
			}

			$commId = $table->getAdapter()->lastInsertId();

			$relationship = new User_Model_Relationship();
			$relationship
				->setId($commId)
				->setFirstUserId($firstAccountId)
				->setSecondUserId($secondAccountId)
				->setType($type)
				->setApproveFlag($approved)
			;

		}

		if ($relationship) {
			self::$_instanceList[$relationship->getId()] = $relationship;
			return $relationship->getId();
		}

		return NULL;
	}

	public static function clear($firstAccountId, $secondAccountId, $type)
	{
		if (intval($firstAccountId) < 1 || intval($secondAccountId) < 1 || empty($type)) {
			return FALSE;
		}

		//TODO: get first

		$table = User_Model_DbTable_Relationship::getInstance();

		$select = $table->select();
		$select
			->where('first_id= ?', $firstAccountId)
			->orWhere('second_id= ?', $firstAccountId)
		;

		$dbResult = $table->fetchAll($select);

		$rowId = 0;

		foreach ($dbResult as $dbRow) {
			if ($dbRow->type === $type
				&& (($dbRow->first_id == $firstAccountId && $dbRow->second_id == $secondAccountId)
				|| ($dbRow->first_id == $secondAccountId && $dbRow->second_id == $firstAccountId))) {

				$rowId = intval($dbRow->id);
				break;
			}
		}

		if ($rowId > 0) {

			$updRows = $table->delete(array(
					'id= ?' => $rowId,
				));

			if ($updRows != 1) {
				return FALSE;
			}

			return TRUE;
		}

		//TODO: clear from the list

		return FALSE;
	}

	public static function _load($accountId)
	{
		if (intval($accountId) < 1) {
			return array();
		}


		$table = User_Model_DbTable_Relationship::getInstance();

		$select = $table->select();
		$select
			->where('first_id= ?', $accountId)
			->orWhere('second_id= ?', $accountId)
		;

		$dbResult = $table->fetchAll($select);

		$instances = array();

		foreach ($dbResult as $dbRow) {
			$i = new User_Model_Relationship();
			$i
				->setId($dbRow->id)
				->setFirstUserId($dbRow->first_id)
				->setSecondUserId($dbRow->second_id)
				->setType($dbRow->type)
				->setApproveFlag(intval($dbRow->approve_flag) === 1)
			;
			$instances[$dbRow->id] = $i;
			self::$_instanceList[$dbRow->id] = $i;
		}


		return $instances;
	}
}
